<template>
  <div>
    <div>
      <el-button type="primary" class="lefta" @click="addStu">添加</el-button>
    </div>
    <el-table type="selection" :data="dangqianData" style="width: 100%">
      <el-table-column prop="stuId" label="ID" width="180"> </el-table-column>
      <el-table-column prop="stuName" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="stuSex" label="性别"> </el-table-column>
      <el-table-column prop="stuAge" label="年龄"> </el-table-column>
      <el-table-column prop="className" label="班级"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" @click="delDataa(scope.row)">删除</el-button>
          <el-button @click="changeStuData(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      :page-size="6"
      layout="prev, pager, next"
      @current-change="changePage"
      :total="pageMax"
    >
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="ID" :label-width="formLabelWidth">
          <el-input v-model="form.stuId" autocomplete="off" disabled></el-input>
        </el-form-item>

        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.stuName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="年龄" :label-width="formLabelWidth">
          <el-input v-model="form.stuAge" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-input v-model="form.stuSex" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="班级" :label-width="formLabelWidth">
          <el-select v-model="form.region" :placeholder="form.className">
            <el-option
              v-for="item in classData"
              :value="item.classId"
              :v-model="item"
              :key="item.classId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateStu()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import * as api from "../utils/api";
// import Tab from "../components/myTab";
export default {
  name: "myDjlStudent",
  components: {
    // Tab,
  },
  data() {
    return {
      tableData: [],
      pageMax: 0,
      dangqianpage: 1,
      dialogTableVisible: false,
      dialogFormVisible: false,
      classData: [],
      title: "添加数据",
      form: {
        stuId: "",
        stuName: "",
        stuAge: "",
        stuSex: "",
        className: "",

        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.axios({
      url: "/getStu",
      method: "get",
    }).then((item) => {
      this.tableData = item.data.data;
      this.pageMax = this.tableData.length;
    });
  },
  methods: {
    delDataa(aa) {
      this.$confirm("此操作将永久删除, 是否继续?", "确认删除？？？", {
        confirmButtonText: "确认删除",
        cancelButtonText: "取消删除",
        type: "warning",
      })
        .then(() => {
          this.axios({
            url: "/delStu",
            method: "post",
            data: {
              stuId: aa.stuId,
            },
          }).then((data) => {
            console.log(data.status);
            if (data.status) {
              this.tableData = data.data.data;
              console.log(this.tableData);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message({
                type: "info",
                message: "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    changePage(val) {
      this.dangqianpage = val;
    },
    changeStuData(obj) {
      this.title = "修改学生";
      let { className, stuId, stuName, stuSex, stuAge } = { ...obj };
      this.form.stuId = stuId;
      this.form.className = className;
      this.form.stuName = stuName;
      this.form.stuSex = stuSex;
      this.form.stuSex = stuSex;
      this.form.stuAge = stuAge;
      this.axios({
        url: "/getClass",
        method: "get",
      }).then((data) => {
        if (data.data.state.code == 200) {
          this.classData = data.data.data;
        }
      });
      this.dialogFormVisible = true;
      console.log(obj);
    },
    updateStu() {
      if (this.title == "修改学生") {
        let stuId = this.form.stuId;
        let stuName = this.form.stuName;
        let stuSex = this.form.stuSex;
        let stuAge = this.form.stuAge;
        let classId = this.form.region;
        let data = {
          stuId: stuId,
          stuName: stuName,
          stuSex: stuSex,
          stuAge: stuAge,
          classId: classId,
        };
        console.log(stuId, stuName, stuSex, stuAge, classId);
        this.dialogFormVisible = false;
        this.axios({
          method: "post",
          url: "/updateStu",
          data: {
            data,
          },
        })
          .then((data) => {
            console.log(data.data.state.code);
            if (data.data.state.code == 200) alert("修改成功");
            this.tableData = data.data.data;
          })
          .catch((err) => {
            alert("修改失败");
          });
      } 
      else {
        
        let stuName = this.form.stuName;
        let stuSex = this.form.stuSex;
        let stuAge = this.form.stuAge;
        let classId = this.form.region;
        let data = {
          // stuId: stuId,
          stuName: stuName,
          stuSex: stuSex,
          stuAge: stuAge,
          classId: classId,
        };
        console.log(stuName, stuSex, stuAge, classId,data);
        this.dialogFormVisible = false;
        this.axios({
          method: "post",
          url: "/changeStu",
          data: {
            data,
          },
        })
          .then((data) => {
            console.log(data.data.state.code);
            if (data.data.state.code == 200) alert("添加成功");
            this.tableData = data.data.data;
            console.log(this.tableData)
          })
          .catch((err) => {
            alert("添加失败");
          });
      }
    },
    addStu() {
      this.title = "添加学生";
      this.axios({
        url: "/getClass",
        method: "get",
      }).then((data) => {
        if (data.data.state.code == 200) {
          this.classData = data.data.data;
        }
      });
      console.log();
      this.form.className = '';
      this.form.stuName = '';
      this.form.stuSex = '';
      this.form.stuSex = '';
      this.form.stuAge = '';
      this.form.stuId = '';
      this.dialogFormVisible = true;
    },
  },

  computed: {
    dangqianData() {
      let dangqian = (this.dangqianpage - 1) * 6;
      let mydata = this.tableData.slice(dangqian, dangqian + 6);
      return mydata;
    },
  },
  watch: {},
};
</script>

<style lang="less">
.lefta {
  float: left;
  width: 200px;
  height: 50px;
}
</style>
